//
//  SDLocalImageLoad.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDLocalImageLoad.h"

#import "SDImageCacheEx.h"
#import "SDFunctionDefine.h"

@implementation SDLocalImageLoad

+ (NSString*)getScaleStr
{
    if ([UIScreen mainScreen].scale > 2)
    {
        return @"@3x";
    }
    if ([UIScreen mainScreen].scale > 1)
    {
        return @"@2x";
    }
    else
    {
        return @"";
    }
}

+ (UIImage*)loadLocalImageWithName:(NSString*)name
{
    if( name == nil )return nil;
    NSString* other = [name stringByDeletingPathExtension];
    NSString* extension = [name pathExtension];
    NSRange range = [other rangeOfString:[SDLocalImageLoad getScaleStr]];
    if( range.location == NSNotFound)
    {
        name = [NSString stringWithFormat:@"%@%@.%@",other,[SDLocalImageLoad getScaleStr],extension];
    }
    
    return [[SDImageCacheEx sharedInstance] getImageWithNameInMainBundle:name];
}

+ (UIImage*)loadLocalImageWithName:(NSString*)name  bundleName:(NSString*)bundle
{
    if(!CHECK_VALID_STRING(name))return nil;
    if (!CHECK_VALID_STRING(bundle))
    {
        return [SDLocalImageLoad loadLocalImageWithName:name];
    }
    NSString* other = [name stringByDeletingPathExtension];
    NSString* extension = [name pathExtension];
    NSRange range = [other rangeOfString:[SDLocalImageLoad getScaleStr]];
    if( range.location == NSNotFound)
    {
        name = [NSString stringWithFormat:@"%@/%@%@.%@",bundle,other,[SDLocalImageLoad getScaleStr],extension];
    }
    
    NSString *scaleStr = [SDLocalImageLoad getScaleStr];
    UIImage *resultImage = [[SDImageCacheEx sharedInstance] getImageWithNameInMainBundle:name];
    
    if (resultImage == nil)
    {
        if ([[SDLocalImageLoad getScaleStr] isEqualToString:@"@3x"])
        {
            scaleStr = @"@2x";
        }
        else
        {
            scaleStr = @"@3x";
        }
    }
    else
    {
        return resultImage;
    }
    if( range.location == NSNotFound)
    {
        name = [NSString stringWithFormat:@"%@/%@%@.%@",bundle,other,scaleStr,extension];
    }
    
    return [[SDImageCacheEx sharedInstance] getImageWithNameInMainBundle:name];
}
@end
